using AutoMapper;
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using Work.API.Applications.Write.Commands;
using Work.Domain.DrugManage;
using Work.ErrorCode;
using Work.Infrastructure.Interfaces;

namespace Work.API.Applications.Write.CommandHandlers
{
    /// <summary>
    /// 更新检药单处理器（用于查看检药单模块）
    /// 业务逻辑：
    /// 1. 验证检药单是否存在
    /// 2. 更新检药单信息（检查状态、检查结果等）
    /// </summary>
    public class UpdateInspectionCommandHandler : IRequestHandler<UpdateInspectionCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<DrugInspection> _inspectionRepo;
        private readonly IMapper _mapper;

        public UpdateInspectionCommandHandler(
            IBaseRepository<DrugInspection> inspectionRepo,
            IMapper mapper)
        {
            _inspectionRepo = inspectionRepo;
            _mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(UpdateInspectionCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();

            try
            {
                // 1. 验证检药单是否存在
                var inspection = await _inspectionRepo.GetById(request.InspectionId);
                if (inspection == null)
                {
                    result.Code = ApiEnum.失败;
                    result.Message = "检药单不存在";
                    result.Data = 0;
                    return result;
                }

                // 2. 更新检药单信息
                if (!string.IsNullOrWhiteSpace(request.InspectionStatus))
                {
                    inspection.InspectionStatus = request.InspectionStatus;
                }

                if (request.InspectionDate.HasValue)
                {
                    inspection.InspectionDate = request.InspectionDate.Value;
                }

                if (!string.IsNullOrWhiteSpace(request.Inspector))
                {
                    inspection.Inspector = request.Inspector;
                }

                if (!string.IsNullOrWhiteSpace(request.InspectionResult))
                {
                    inspection.InspectionResult = request.InspectionResult;
                }

                if (!string.IsNullOrWhiteSpace(request.HandlingMethod))
                {
                    inspection.HandlingMethod = request.HandlingMethod;
                }

                if (request.IsDispensed.HasValue)
                {
                    inspection.IsDispensed = request.IsDispensed.Value;
                    if (request.IsDispensed.Value && request.DispensedDate.HasValue)
                    {
                        inspection.DispensedDate = request.DispensedDate.Value;
                    }
                }

                if (!string.IsNullOrWhiteSpace(request.Remarks))
                {
                    inspection.Remarks = request.Remarks;
                }

                var rows = await _inspectionRepo.UpdateAsync(inspection);

                if (rows > 0)
                {
                    result.Code = ApiEnum.成功;
                    result.Message = "检药单更新成功";
                    result.Data = inspection.InspectionId;
                }
                else
                {
                    result.Code = ApiEnum.失败;
                    result.Message = "检药单更新失败";
                    result.Data = 0;
                }
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.失败;
                result.Message = $"更新失败：{ex.Message}";
                result.Data = 0;
            }

            return result;
        }
    }
}

